package com.itzzj.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public Boolean setBit(String key,long offset,boolean isShow){
        boolean result = false;
        try {
            ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
            result = opsForValue.setBit(key, offset, isShow);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public boolean getBit(String key,long offset){
        boolean result = false;
        try {
            ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
            result = opsForValue.getBit(key,offset);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public boolean set(final String key,Object value){
        boolean result = false;
        try {
            ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
            opsForValue.set(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置缓存的过期时间
     * @param key
     * @param value
     * @param expireTime 过期时间以秒为单位
     * @return
     */
    public boolean set(final String key,Object value,Long expireTime){
        boolean result = false;
        try {
            ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
            opsForValue.set(key,value,expireTime,TimeUnit.SECONDS);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据 key 删除对应的 value
     * @param key
     */
    public void remove(final String key){
        redisTemplate.delete(key);
    }

    /**
     * 根据 key 批量删除对应的 value
     * @param keys
     */
    public void remove(final String... keys){
        redisTemplate.delete(Arrays.asList(keys));
    }

    /**
     * 判断缓存中是否存在对应的 key
     * @param key
     * @return
     */
    public boolean exists(final String key){
        return redisTemplate.hasKey(key);
    }

    public Object get(final String key){
        Object result = redisTemplate.opsForValue().get(key);
        return result;
    }

    /**
     * 哈希添加
     * @param key
     * @param hashKey
     * @param val
     */
    public void hmSet(String key,Object hashKey,Object val){
        redisTemplate.opsForHash().put(key,hashKey,val);
    }

    /**
     * 哈希获取数据
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key,Object hashKey){
        Object result = redisTemplate.opsForHash().get(key, hashKey);
        return result;
    }

    /**
     * 列表添加
     * @param key
     * @param val
     */
    public void lPush(String key,Object val){
        redisTemplate.opsForList().rightPush(key,val);
    }

    /**
     * 范围获取
     * 获取 key 的列表中从 l 开始的数据到 l1 结束的数据
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lRange(String k,long l,long l1){
        return redisTemplate.opsForList().range(k,l,l1);
    }

    /**
     * 集合添加
     * @param key
     * @param val
     */
    public void add(String key,Object val){
        redisTemplate.opsForSet().add(key,val);
    }

    /**
     * 集合添加
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key){
       return redisTemplate.opsForSet().members(key);
    }

    /**
     * 有序集合添加
     * @param key
     * @param val
     * @param score 分数
     */
    public void zAdd(String key,Object val,double score){
        redisTemplate.opsForZSet().add(key,val,score);
    }

    /**
     * 有序集合获取
     * 从集合排序中得到分数介于 score 和 score1 之间的元素
     * @param key
     * @param score
     * @param score1
     * @return
     */
    public Set<Object> rangeByScore(String key,double score,double score1){
        return redisTemplate.opsForZSet().rangeByScore(key,score,score1);
    }

    /**
     * 有序集合获取排名
     * @param key
     * @param val
     * @return
     */
    public Long zRank(String key,Object val){
       return redisTemplate.opsForZSet().rank(key,val);
    }

}















































